package com.suning.sawp.service.impl.logistics;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suning.rsc.dto.responsedto.MbfResponse;
import com.suning.rsc.httpservice.annotation.EsbEIHttpWired;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.dto.esb.QueryOrderPageReq;
import com.suning.sawp.dto.esb.QueryOrderPageRtn;
import com.suning.sawp.intf.esb.QueryOrderESBService;
import com.suning.sawp.intf.logistics.QueryOrderPageService;
import com.suning.sawp.service.util.XmlUtils;
import com.suning.vgs.follow.remote.intf.StoreManRemoteService;
import com.suning.vgs.follow.remote.store.ReturnMsg;
import com.suning.vgs.follow.remote.store.StoreManBaseDto;

/**
 * 
 * 分页查询订单列表<br>
 * 〈功能详细描述〉
 * 
 * @author Wuxiao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class QueryOrderPageServiceImpl implements QueryOrderPageService {

    private static final Logger  LOGGER        = LoggerFactory
                                                       .getLogger(LoggerConstants.SAWP_INTEGRATION);

    /**
     * 促销员岗位名称 写死
     */
    private static final String  PROMOTION_NAME = "促销员";

    public static final String   USER_PREFIX_Y = "Y";                                                // 营业员前缀

    public static final String   USER_PREFIX_Z = "Z";                                                // 促销员前缀

    @Autowired
    StoreManRemoteService        storeManRemoteService;

    private QueryOrderESBService queryOrderESBService;

    public QueryOrderESBService getQueryOrderESBService() {
        return queryOrderESBService;
    }

    @EsbEIHttpWired
    public void setQueryOrderESBService(QueryOrderESBService queryOrderESBService) {
        this.queryOrderESBService = queryOrderESBService;
    }

    @Override
    public QueryOrderPageRtn queryOrderInfoByPage(String oaAccountNo, String storeCode,
            String telNo, String startTime, String endTime, Integer start, Integer limit) {
        QueryOrderPageRtn rtn = new QueryOrderPageRtn();
        QueryOrderPageReq req = new QueryOrderPageReq();
        req.setStoreCode(storeCode);
        req.setEndDate(endTime);
        req.setStartDate(startTime);
        req.setPerPageRecordNumber(String.valueOf(limit));
        req.setCurrentPageNumber(String.valueOf(start / limit + 1));
        if (StringUtils.isNotBlank(telNo)) {
            req.setMobilePhoneNum(telNo);
        } else {
            ReturnMsg<StoreManBaseDto> storeManInfoWhenLogin = storeManRemoteService
                    .getStoreManInfoWhenLogin(oaAccountNo);
            if (storeManInfoWhenLogin != null
                    && storeManInfoWhenLogin.getData().getStationName() != null
                    && storeManInfoWhenLogin.getData().getStationName().contains(PROMOTION_NAME)) {
                req.setSalesPerson(USER_PREFIX_Z + oaAccountNo);
            }else{
                req.setSalesPerson(USER_PREFIX_Y + oaAccountNo);
            }
        }
        MbfResponse mbfResponse = null;
        try {
            mbfResponse = queryOrderESBService.queryOrderByPage(req);
            LOGGER.debug("调用OMS查询订单列表响应报文{}", mbfResponse.toString());
        } catch (Exception e) {
            LOGGER.error("调用OMS查询订单列表异常,storeCode:" + oaAccountNo + ",storeCode:" + storeCode
                    + ",telNo:" + telNo + ",startTime:" + startTime + ",endTime:" + endTime
                    + ",start:" + start + ",limit:" + limit, e);
            rtn.setOperateFlag(QueryOrderPageRtn.FAIL);
            rtn.setErrorCode(ErrorCodeConstants.QUERY_ORDER_ERR_E002);
            rtn.setErrorMassage(ErrorCodeConstants.QUERY_ORDER_ERR_MAP
                    .get(ErrorCodeConstants.QUERY_ORDER_ERR_E002));
        }
        if (null != mbfResponse && null != mbfResponse.getOutput() && XmlUtils.isEsbRespSuccess(mbfResponse)) {
            try {
                rtn = (QueryOrderPageRtn) mbfResponse.getOutput().getMbfBody(
                        QueryOrderPageRtn.class);
            } catch (Exception e) {
                LOGGER.error("调用OMS查询订单列表接口解析报文异常", e);
            }
        }
        return rtn;
    }

}
